﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace Core
{
    public static partial class system
    {
        public static dynamic addOrEditLangInfo(int code, int? oldCode, string title, bool disabled,
            bool standardTest, string standardCode, int? standardTime, int? standardMem,
            bool safeTest, string safeRegExp)
        {
            var sql = new Sql.SqlDataContext();
            Sql.Language l;
            if (oldCode != null)
            {
                l = sql.Language.FirstOrDefault(row => row.code == oldCode);
                if (l == null) return new
                    {
                        success = false,
                        msg = "语言不存在"
                    };
                if (code != oldCode)
                {
                    // Update Problem SpecialJudge
                    foreach (var p in sql.Problem.Where(row => row.specialLang == oldCode))
                        p.specialLang = code;
                    // Update Problem Example
                    foreach (var p in sql.Problem.Where(row => row.exampleLang == oldCode))
                        p.exampleLang = code;
                    // Update Status
                    foreach (var s in sql.Status.Where(row => row.lang == oldCode))
                        s.lang = code;
                }
            }
            else l = new Sql.Language();
            l.code = code;
            l.title = title;
            l.disabled = disabled;
            l.standardTest = standardTest;
            l.standardCode = standardCode;
            l.standardTime = standardTime;
            l.standardMem = standardMem;
            l.safeTest = safeTest;
            l.safeRegExp = safeRegExp;
            if (oldCode == null) sql.Language.InsertOnSubmit(l);
            sql.SubmitChanges();
            return new
            {
                success = true
            };
        }
    }
}
